<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="询价id" prop="inquiryId">
        <el-input
          v-model="queryParams.inquiryId"
          placeholder="请输入询价id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="询报价需求" prop="inquiryDemand">
        <el-input
          v-model="queryParams.inquiryDemand"
          placeholder="请输入询报价需求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报价截止时间" prop="quoteDeadline">
        <el-date-picker clearable
          v-model="queryParams.quoteDeadline"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报价截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inquiry:demand:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inquiry:demand:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inquiry:demand:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="demandList" @selection-change="handleSelectionChange" style="width: 1664px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="询报价需求编号" align="center" prop="inquiryDemandNumber" width="180"/>
<!--      <el-table-column label="询价id" align="center" prop="inquiryId" />-->
<!--      <el-table-column label="询报价需求" align="center" prop="inquiryDemand" />-->
      <el-table-column label="采购明细">
        <el-table-column label="采购申请编号"  width="180">
          <template slot-scope="scope" >
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{requestnumber.purchaseRequestNumber}}
            </div>

          </template>
        </el-table-column>
        <el-table-column label="需求行号" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{requestnumber.demandLineNumber}}

            </div>
          </template>
        </el-table-column>
        <el-table-column label="需求行号状态" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 142px;padding: 15px 0px 16px 0px;text-align: center">
              <dict-tag :options="dict.type.demand_line_status" :value="requestnumber.demandLineStatus"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="采购类型" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              <dict-tag :options="dict.type.sys_type_purchase" :value="requestnumber.productNumbera.productCategory"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="申请日期" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(user ,index) in scope.row.pcmApplicationDetailList" style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{ parseTime(user.applicationDate,'{y}-{m}-{d}')}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="申请人" width="140" align="center">
            <template slot-scope="scope">
              <div v-for="(user ,index) in scope.row.pcmApplicationDetailList" style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
                {{user.userlist.userName}}
              </div>
            </template>
        </el-table-column>
        <el-table-column label="申请部门" width="160" align="center">
          <template slot-scope="scope">
            <div v-for="(user ,index) in scope.row.pcmApplicationDetailList" style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{user.userlist.dept.deptName}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="产品编号" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{requestnumber.productNumbera.productNumber}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="产品名称" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{requestnumber.productNumbera.productName}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="单位" width="140" align="center">
          <template slot-scope="scope">

            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              <dict-tag :options="dict.type.sys_unit" :value="requestnumber.productNumbera.unit"/>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="需求数量" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{requestnumber.requiredQuantity}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="期望到货日期" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              {{requestnumber.expectedDeliveryDate}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="期望供应商" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 18px 0px;text-align: center">
              <span style="display: inline-block;height: 10px">{{requestnumber.pcmSupplier.supplierFullName}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="供应商状态" width="140" align="center">
          <template slot-scope="scope">
            <div v-for="(requestnumber,index) in scope.row.pcmApplicationDetailList"  style="border-top: 1px #dfe6ec solid;width: 100%;padding: 15px 0px 16px 0px;text-align: center">
              <span v-if="requestnumber.pcmSupplier.supplierstates==null" style="display: inline-block;height: 21px; "><dict-tag :options="dict.type.pcm_supplier_states" :value="requestnumber.pcmSupplier.supplierstates"/></span>
              <span v-else ><dict-tag :options="dict.type.pcm_supplier_states" :value="requestnumber.pcmSupplier.supplierstates"/></span>
            </div>
          </template>
        </el-table-column>

      </el-table-column>
      <el-table-column label="报价截止时间" align="center" prop="quoteDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.quoteDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交人" align="center" prop="userlist.userName" width="140"/>
      <el-table-column label="提交时间" align="center" prop="submitDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
<el-dialog :title="title2" :visible.sync="flag" width="1035px" append-to-body>
  <el-form :model="queryDetal" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="申请编号" prop="purchaseRequestNumber">
      <el-input
        v-model="queryDetal.purchaseRequestNumber"
        placeholder="请输入采购申请编号"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="产品编号" prop="productNumber">
      <el-input
        v-model="queryDetal.productNumber"
        placeholder="请输入采购产品编号"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="产品名称" prop="productName">
      <el-input
        v-model="queryDetal.productName"
        placeholder="请输入采购产品名称"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>
  <el-table v-loading="loading" :data="pcmApplicationDetailList" @selection-change="handleSelectionChange" border style="width: 900px">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="采购申请行状态" width="140" align="center">
      <template slot-scope="scope">
          <dict-tag :options="dict.type.demand_line_status" :value="scope.row.demandLineStatus"/>
      </template>
    </el-table-column>
    <el-table-column label="采购申请编号" align="center" prop="purchaseRequestNumber" width="180"/>
    <el-table-column label="产品编号" align="center" prop="productNumber" width="180"/>
    <el-table-column label="产品名称" align="center" prop="productName" width="140"/>
    <el-table-column label="单位" align="center" prop="unit" width="140"/>
    <el-table-column label="需求行号" align="center" prop="demandLineNumber" width="140"/>
    <el-table-column label="需求数量" align="center" prop="requiredQuantity" width="140"/>
    <el-table-column label="期望到货日期" align="center" prop="expectedDeliveryDate" width="140" />
    <el-table-column label="期望供应商" align="center" prop="pcmSupplier.supplierFullName" width="140"/>
    <el-table-column label="供应商状态" align="center" prop="purchaseRequestNumber" width="140">
      <template slot-scope="scope">

          <dict-tag :options="dict.type.pcm_supplier_states" :value="scope.row.pcmSupplier.supplierstates"/>

      </template>
    </el-table-column>
    <el-table-column label="操作" align="center" width="180">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)"
          v-hasPermi="['system:demand:edit']"
        >修改</el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleDelete(scope.row)"
          v-hasPermi="['system:demand:remove']"
        >删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <pagination
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
  />
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="subapp" :disabled="multiple">确 定</el-button>
    <el-button @click="cancel">取 消</el-button>
  </div>
</el-dialog>
    <!-- 添加或修改询报价需求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">

        <el-form-item label="询报价需求编号" prop="inquiryDemand">
          <el-input v-model="form.inquiryDemandNumber" :disabled="isDe"  placeholder="需求编号自动生成" />
        </el-form-item>

        <el-divider content-position="center">申请明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPcmApplicationDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePcmApplicationDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="pcmApplicationDetailLists" :row-class-name="rowPcmApplicationDetailIndex" @selection-change="handlePcmApplicationDetailSelectionChange" ref="pcmApplicationDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="采购申请编号" prop="purchaseRequestNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.purchaseRequestNumber" placeholder="选择采购申请编号" @focus="shenqingxiangq()"/>
            </template>
          </el-table-column>
          <el-table-column label="需求行号状态" prop="demandLineStatus" width="150">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.demand_line_status" :value="scope.row.demandLineStatus"/>
            </template>
          </el-table-column>

          <el-table-column label="产品编号" prop="productNumber" width="150"/>
          <el-table-column label="产品名称" align="center" prop="productName" width="140"/>
          <el-table-column label="单位" align="center" prop="unit" width="140">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_unit" :value="scope.row.unit"/>
            </template>
          </el-table-column>
          <el-table-column label="需求数量" prop="requiredQuantity" width="150"/>
          <el-table-column label="期望到货日期" prop="expectedDeliveryDate" width="240"/>
          <el-table-column label="期望供应商" prop="pcmSupplier.supplierFullName" width="150"/>
          <el-table-column label="供应商状态" prop="pcmSupplier.supplierstates" width="150">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.pcm_supplier_states" :value="scope.row.pcmSupplier.supplierstates"/>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 30px 0px"></div>
        <el-form-item label="报价截止时间" prop="quoteDeadline">
          <el-date-picker clearable
                          v-model="form.quoteDeadline"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择报价截止时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancela">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDemand, getDemand, delDemand, addDemand, updateDemand, listApplication} from "@/api/inquiry/demand";
import dict from "@/utils/dict";
import {parseTime} from "../../utils/ruoyi";

export default {
  name: "Demand",
  dicts: ['pcm_supplier_states','sys_type_purchase','demand_line_status','sys_unit'],
  data() {
    return {
      // 遮罩层
      loading: true,
      isDe:true,
      // 选中数组
      ids: [],
      title2:"",
      flag:false,
      // 子表选中数据
      checkedPcmApplicationDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 询报价需求表格数据
      demandList: [],
      // 申请明细表格数据
      pcmApplicationDetailList: [],
      pcmApplicationDetailLists:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        inquiryId: null,
        inquiryDemand: null,
        quoteDeadline: null
      },
      queryDetal:{
        pageNum:1,
        pageSize:5,
        purchaseRequestNumber:null,
        productNumber:null,
        productName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      id:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询询报价需求列表 */
    getList() {
      this.loading = true;
      listDemand(this.queryParams).then(response => {
        this.demandList = response.rows;
        this.total = response.total;
        console.log(response.rows)
        this.loading = false;
      });
    },
    /**查询采购明细列表**/
    getApplication(){
      this.loading=true;
      listApplication(this.queryDetal).then(response=>{
        this.pcmApplicationDetailList=response.rows;
        this.total=response.total;
        console.log(response.rows)
        this.loading=false;
      })
    },
    handleSelectionmx(selections){
      this.pcmApplicationDetailLists=selections.map(item=>item.purchaseRequestNumber);
      ///selection.map()
      console.log("121"+selections);
    },
    // 取消按钮
    cancel() {
      this.flag = false;
      this.reset();
    },
    cancela() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inquiryDemandNumber: null,
        inquiryId: null,
        inquiryDemand: null,
        quoteDeadline: null
      };
      this.pcmApplicationDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inquiryDemandNumber)
      this.id=selection.map(item=>item.inquiryId);
      console.log(this.id[0])
      if (this.id[0]===undefined){
        this.pcmApplicationDetailLists=selection
      }
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加询报价需求";
    },
    shenqingxiangq(){

      console.log("111111111");
      this.flag = true;
      this.getApplication();
      console.log(this.flag);
      this.title2 = "申请明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inquiryDemandNumber = row.inquiryDemandNumber || this.ids
      getDemand(inquiryDemandNumber).then(response => {
        this.form = response.data;
        this.pcmApplicationDetailList = response.data.pcmApplicationDetailList;
        this.open = true;
        this.title = "修改询报价需求";
      });
    },
    subapp(row){

      this.flag=false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.pcmApplicationDetailList = this.pcmApplicationDetailLists;
          if (this.form.detailId != null) {
            updateDemand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
           console.log( this.form)
            addDemand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inquiryDemandNumbers = row.inquiryDemandNumber || this.ids;
      this.$modal.confirm('是否确认删除询报价需求编号为"' + inquiryDemandNumbers + '"的数据项？').then(function() {
        return delDemand(inquiryDemandNumbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 申请明细序号 */
    rowPcmApplicationDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 申请明细添加按钮操作 */
    handleAddPcmApplicationDetail() {
      let obj = {};
      obj.purchaseRequestNumber = "";
      obj.productNumber = "";
      obj.demandLineNumber = "";
      obj.requiredQuantity = "";
      obj.expectedDeliveryDate = "";
      obj.pcmSupplier=[];
      obj.pcmSupplier.supplierFullName="";
      obj.pcmSupplier.supplierstatus="";
      obj.demandLineStatus = "";
      obj.requestId = "";
      this.pcmApplicationDetailLists.push(obj);
    },
    /** 申请明细删除按钮操作 */
    handleDeletePcmApplicationDetail() {
      if (this.checkedPcmApplicationDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的申请明细数据");
      } else {
        const pcmApplicationDetailList = this.pcmApplicationDetailList;
        const checkedPcmApplicationDetail = this.checkedPcmApplicationDetail;
        this.pcmApplicationDetailList = pcmApplicationDetailList.filter(function(item) {
          return checkedPcmApplicationDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePcmApplicationDetailSelectionChange(selection) {
      this.checkedPcmApplicationDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/demand/export', {
        ...this.queryParams
      }, `demand_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
::v-deep .el-table .cell{
  padding: 0px;
}
::v-deep .el-table--medium .el-table__cell{
  padding: 0px;
}

</style>
